' Copyright 2006 ESRI
' 							  
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See use restrictions at /arcgis/developerkit/userestrictions.


Imports System
Imports System.Text
Imports System.Web
Imports System.Data
Imports System.Configuration
Imports System.IO


Namespace CustomToolLibrary


    Public Class downloadData

#Region "downloadData"

        Sub DownloadInformation(ByVal QueryString As String, ByVal FilePath As String)
            Dim query As New StringBuilder
            Dim myFileString = New StringBuilder()

            myFileString.Append(FilePath)
            myFileString.Append(HttpContext.Current.User.Identity.Name.ToString())
            myFileString.Append("\")
            myFileString.Append(DateTime.Now.Year.ToString())
            myFileString.Append(DateTime.Now.Month.ToString())
            myFileString.Append(DateTime.Now.Day.ToString())
            myFileString.Append(DateTime.Now.Hour.ToString())
            myFileString.Append(DateTime.Now.Minute.ToString())
            myFileString.Append(DateTime.Now.Second.ToString())
            myFileString.Append(DateTime.Now.Millisecond.ToString())
            myFileString.Append("\")

            If Not Directory.Exists(myFileString.ToString()) Then
                Directory.CreateDirectory(myFileString.ToString())
            End If

            query.Append("INSERT INTO WDPA.dbo.Download(UserID, Query_String, File_Path, Status, Status_Message, Export_Format) VALUES (")
            query.Append("'" + HttpContext.Current.User.Identity.Name.ToString() + "', ")
            query.Append("'" + QueryString + "', ")
            query.Append("'" + myFileString.ToString() + "', ")
            query.Append("'1', ")
            query.Append("'No Problems', ")
            query.Append("'SHP') ")

            AddTandCtoFolder(FilePath, myFileString.ToString())

            Dim dbConn As String = ConfigurationManager.ConnectionStrings("WDPAConnectionString1").ConnectionString
            Dim dbReader As System.Data.SqlClient.SqlDataReader
            Dim sqlConn As New System.Data.SqlClient.SqlConnection(dbConn)
            sqlConn.Open()
            Dim sqlCommand As New System.Data.SqlClient.SqlCommand(query.ToString(), sqlConn)
            dbReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection)

        End Sub

        Private Sub AddTandCtoFolder(ByVal TandCpath As String, ByVal FolderPath As String)

            Dim Targetpath As String = FolderPath + "WDPA Terms and Conditions of use.txt"
            Dim Sourcepath2 As String = TandCpath + "WDPA Terms and Conditions of use.txt"

            'Dim fs As FileStream = File.Create(Targetpath)
            'fs.Close()

            '' Ensure that the target does not exist.
            'File.Delete(Targetpath)

            ' Copy the file.
            'File.Copy(Sourcepath2, Targetpath, True)

        End Sub

#End Region
    End Class 'downloadData


End Namespace 'CustomToolLibrary